package com.hdculture.simple.expensemanager.premium;

import com.hdculture.simple.expensemanager.premium.R;

import android.app.Activity;
import android.app.Dialog;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.view.*;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.content.Context;

public class PreferencesActivity extends Activity {
    /** Called when the activity is first created. */
	ListView choiceList;
	Button backButton;
	CoreDBAdapter adapter;
		String[] choices = {"Reset Database","Change Password","Edit Currency"};
		

		@Override
		protected void onPause() {
			// TODO Auto-generated method stub
	    	adapter.close();
			super.onPause();
		}
		@Override
		protected void onResume() {
			// TODO Auto-generated method stub
			adapter.open();
			super.onResume();
		}
		
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preferences);
        choiceList = (ListView)findViewById(R.id.preferencesList);
        adapter = new CoreDBAdapter(getApplicationContext());
		adapter.open();
		backButton = (Button)findViewById(R.id.backButton);
		backButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				adapter.close();
				finish();
			}
		});
        initializePreferencesList();
    }
    public void initializePreferencesList(){
    	choiceList.setAdapter(new ArrayAdapter<String>(PreferencesActivity.this,android.R.layout.simple_list_item_1,choices));
    	choiceList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				switch(arg2){
				case 0:	//Reset Database...
						adapter.resetDB();
						adapter.close();
						Toast.makeText(getApplicationContext(),"Success!",1000).show();
						break;
				case 1:	//Change Password
						final Dialog changePassDialog = new Dialog(PreferencesActivity.this);
						changePassDialog.setContentView(R.layout.changepassworddialog);
						changePassDialog.setTitle("Change Password");
						final Button okButton = (Button)changePassDialog.findViewById(R.id.okButton);
						final Button cancelButton  = (Button)changePassDialog.findViewById(R.id.cancelButton);
						okButton.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View arg0) {
								final EditText oldPasswordText= (EditText)changePassDialog.findViewById(R.id.oldPasswordText);
								final EditText newPassword1Text = (EditText)changePassDialog.findViewById(R.id.newPassword1Text);
								final EditText newPassword2Text = (EditText)changePassDialog.findViewById(R.id.newPassword2Text);
								String old = oldPasswordText.getText().toString();
								String new1 =newPassword1Text.getText().toString();
								String new2 = newPassword2Text.getText().toString();
								boolean isOldCorrect = adapter.verifyPassword(old);
								if(isOldCorrect==true){
									if(new1.equals(new2)){
										adapter.updatePassword(new1);
										Toast.makeText(PreferencesActivity.this,"Password successfully updated!",1000).show();
										changePassDialog.dismiss();
									}else{
										Toast.makeText(PreferencesActivity.this,"Passwords dont match.",1000).show();
									}
								}else{
									Toast.makeText(PreferencesActivity.this,"Incorrect old password. Please try again.",1000).show();
								}
							}
						});
						
						//final Button closeButton = (Button)findViewById(R.id.cancelButton);
						cancelButton.setOnClickListener(new View.OnClickListener() {
							
							@Override
							public void onClick(View arg0) {
								changePassDialog.dismiss();
							}
						});
						changePassDialog.show();
						
						break;
				case 2:	//Edit Currency
						final Dialog cDialog = new Dialog(PreferencesActivity.this);
						cDialog.setTitle("Edit Currency Symbol");
						cDialog.setContentView(R.layout.changecurrencydialog);
						final EditText sign = (EditText)cDialog.findViewById(R.id.currencySign);
						final Button closeButton = (Button)cDialog.findViewById(R.id.cancelButton);
						final Button continueButton = (Button)cDialog.findViewById(R.id.okButton);
						continueButton.setOnClickListener(new View.OnClickListener() {
							
							@Override
							public void onClick(View arg0) {
								adapter.updateCurrency(sign.getText().toString());
								Toast.makeText(PreferencesActivity.this,"Currency Sign updated!",1000).show();
							}
						});
						closeButton.setOnClickListener(new View.OnClickListener() {
							
							@Override
							public void onClick(View arg0) {
								cDialog.dismiss();
							}
						});
						cDialog.show();
						break;
				}
			}
		});
    }
}